﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using Tesla.Core;

namespace Tesla.Input {
    /// <summary>
    /// InputTriggers are defined by a condition and an
    /// action. If the condition is satisfied, the action
    /// is triggered.
    /// </summary>
    public sealed class InputTrigger {
        private InputCondition _condition;
        private InputAction _action;
        private bool _isEnabled;

        /// <summary>
        /// Get or set the condition of this trigger.
        /// </summary>
        public InputCondition Condition {
            get {
                return _condition;
            }
            set {
                _condition = value;
            }
        }

        /// <summary>
        /// Get or set the action of this trigger.
        /// </summary>
        public InputAction Action {
            get {
                return _action;
            }
            set {
                _action = value;
            }
        }

        /// <summary>
        /// Get or set if this trigger is enabled.
        /// </summary>
        public bool IsEnabled {
            get {
                return _isEnabled;
            }
            set {
                _isEnabled = value;
            }
        }

        /// <summary>
        /// Creates a new InputTrigger with the specified condition and action. It
        /// is enabled by default.
        /// </summary>
        /// <param name="condition">Trigger's condition</param>
        /// <param name="action">Trigger's action</param>
        public InputTrigger(InputCondition condition, InputAction action) {
            _condition = condition;
            _action = action;
            _isEnabled = true;
        }

        /// <summary>
        /// Checks the condition and performs the action
        /// if the condition is satisfied.
        /// </summary>
        /// <param name="time">Time elapsed since the last update</param>
        /// <returns>True if the action successfully was performed, false otherwise</returns>
        public bool CheckAndPerform(GameTime time) {
            if(!_isEnabled || _condition == null || _action == null) {
                return false;
            }

            if(_condition.Check(time)) {
                _action.Perform(time);
                return true;
            }

            return false;
        }
    }
}
